<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2005/08/07 05:20:29 $)
// $RCSfile: functions_importexport.php,v $ : $Revision: 1.5 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}


/**
* Exports troubleshooter categories
*/
function exportCategories($troubleshootercatidlist)
{
	global $dbCore, $_SWIFT, $xml;

	if (!_is_array($troubleshootercatidlist))
	{
		return false;
	}

	$troubleshootercatlist = $troubleshootersteplist = $troubleshooterlinklist = array();

	// get all the categories
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."troubleshootercat` AS troubleshootercat WHERE troubleshootercat.troubleshootercatid IN (". buildIN($troubleshootercatidlist) .");");
	while ($dbCore->nextRecord())
	{
		$troubleshootercatlist[$dbCore->Record["troubleshootercatid"]] = $dbCore->Record;
	}

	// Get the steps
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."troubleshootersteps` AS troubleshootersteps LEFT JOIN `". TABLE_PREFIX ."troubleshooterdata` AS troubleshooterdata ON (troubleshootersteps.troubleshooterid = troubleshooterdata.troubleshooterid) WHERE troubleshootersteps.troubleshootercatid IN (". buildIN($troubleshootercatidlist) .") ORDER BY troubleshootersteps.troubleshooterid;");
	while ($dbCore->nextRecord())
	{
		$troubleshootersteplist[$dbCore->Record["troubleshooterid"]] = $dbCore->Record;
	}

	// Get the links
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."troubleshooterlinks` WHERE `troubleshootercatid` IN (". buildIN($troubleshootercatidlist) .") ORDER BY `troubleshooterlinkid` ASC;");
	while ($dbCore->nextRecord())
	{
		$troubleshooterlinklist[$dbCore->Record["troubleshooterlinkid"]] = $dbCore->Record;
	}

	$xml->addComment(sprintf($_SWIFT["language"]["generationdate"], edate($_SWIFT["settings"]["dt_datetimeformat"], DATENOW)));
	$xml->addParentTag("troubleshooter");

	// ======= CATEGORIES =======
	$xml->addComment("### BEGIN CATEGORIES ###");
	foreach ($troubleshootercatlist as $key=>$val)
	{
		$xml->addParentTag("category");
		$xml->addTag("troubleshootercatid", $val["troubleshootercatid"]);
		$xml->addTag("title", $val["title"]);
		$xml->addTag("description", $val["description"]);
		$xml->addTag("displayorder", $val["displayorder"]);
		$xml->endParentTag("category");
	}
	$xml->addComment("### END CATEGORIES ###");

	// ======= STEPS =======
	$xml->addComment("### BEGIN STEPS ###");
	foreach ($troubleshootersteplist as $key=>$val)
	{
		$xml->addParentTag("step");
		$xml->addTag("troubleshooterid", $val["troubleshooterid"]);
		$xml->addTag("troubleshootercatid", $val["troubleshootercatid"]);
		$xml->addTag("subject", $val["subject"]);
		$xml->addTag("displayorder", $val["displayorder"]);
		$xml->addTag("contents", $val["contents"]);
		$xml->endParentTag("step");		
	}
	$xml->addComment("### END STEPS ###");

	// ======= LINKS =======
	$xml->addComment("### BEGIN LINKS ###");
	foreach ($troubleshooterlinklist as $key=>$val)
	{
		$xml->addParentTag("link");
		$xml->addTag("troubleshootercatid", $val["troubleshootercatid"]);
		$xml->addTag("parenttroubleshooterid", $val["parenttroubleshooterid"]);
		$xml->addTag("childtroubleshooterid", $val["childtroubleshooterid"]);
		$xml->endParentTag("link");		
	}
	$xml->addComment("### END LINKS ###");

	$xml->endParentTag("troubleshooter");

	$xmldata = $xml->returnXML();

	$filename = "troubleshooter.xml";

	if(isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/MSIE/", $_SERVER['HTTP_USER_AGENT'])) {
		// IE Bug in download name workaround
		ini_set( 'zlib.output_compression','Off' );
	}

	header("Content-Type: application/force-download");

	if (strpos($_SERVER['HTTP_USER_AGENT'], "MSIE")){
		header("Content-Disposition: attachment; filename=\"". $filename ."\"");
	} else{
		header("Content-Disposition: attachment; filename=\"". $filename ."\"");
	}

	header("Content-Transfer-Encoding: binary");
	header("Content-Length: ". strlen($xmldata));

	echo $xmldata;

	return true;
}

/**
* Import the troubleshooter data
*/
function importTroubleshooter($filename)
{
	global $_SWIFT, $xml, $dbCore, $datastore;

	// Parse the troubleshooter XML File.
	$xmldata = $xml->xml2tree(implode("", file($filename)));
	$ft = &$xmldata["troubleshooter"][0]["children"];

	$tgroupidlist = $_catpointer = $_steppointer = $linksql = array();

	$dbCore->query("SELECT `tgroupid` FROM `". TABLE_PREFIX ."templategroups` ORDER BY `tgroupid` ASC;");
	while ($dbCore->nextRecord())
	{
		$tgroupidlist[] = $dbCore->Record["tgroupid"];
	}

	// First lets process all the categories and insert em
	for ($ii=0; $ii<count($ft["category"]); $ii++)
	{
		$category = &$ft["category"][$ii]["children"];
		$troubleshootercatid = $category["troubleshootercatid"][0]["values"][0];
		$title = $category["title"][0]["values"][0];
		$description = $category["description"][0]["values"][0];
		$displayorder = $category["displayorder"][0]["values"][0];
		$contents = $category["contents"][0]["values"][0];
		$newtroubleshootercatid = insertTroubleshooterCat($title, $description, $displayorder, $tgroupidlist, $contents, true);
		$_catpointer[$troubleshootercatid] = $newtroubleshootercatid;
	}

	// Now we process the steps
	for ($ii=0; $ii<count($ft["step"]); $ii++)
	{
		$step = &$ft["step"][$ii]["children"];
		$troubleshooterid = $step["troubleshooterid"][0]["values"][0];
		$troubleshootercatid = $step["troubleshootercatid"][0]["values"][0];
		$subject = $step["subject"][0]["values"][0];
		$displayorder = $step["displayorder"][0]["values"][0];
		$contents = $step["contents"][0]["values"][0];
		$newcat = $_catpointer[$troubleshootercatid];

		$newtroubleshooterid = insertStep($newcat, $subject, $displayorder, $contents, array());
		$_steppointer[$troubleshooterid] = $newtroubleshooterid;
	}

	// Once we have all the steps inserted, we insert the required links.. this is a bit complex.
	for ($ii=0; $ii<count($ft["link"]); $ii++)
	{
		$link = &$ft["link"][$ii]["children"];
		$troubleshootercatid = $link["troubleshootercatid"][0]["values"][0];
		$parenttroubleshooterid = $link["parenttroubleshooterid"][0]["values"][0];
		$childtroubleshooterid = $link["childtroubleshooterid"][0]["values"][0];
		if ($parenttroubleshooterid == "NULL")
		{
			$parenttroubleshooterid = 0;
		}
		if ($parenttroubleshooterid == 0)
		{
			$newparent = 0;
		} else {
			$newparent = $_steppointer[$parenttroubleshooterid];
		}
		$newchild = $_steppointer[$childtroubleshooterid];
		$newcat = $_catpointer[$troubleshootercatid];
		// Sanity check
		if (trim($newcat) == "" || trim($newchild) == "" || trim($newparent) == "")
		{
			continue;
		}

		// Ok, now we prepeare the link sql
		$linksql[] = "('". intval($newcat) ."', '". intval($newparent) ."', '". intval($newchild) ."')";
	}

	if (count($linksql))
	{
		$dbCore->shutdownQuery("REPLACE INTO `". TABLE_PREFIX ."troubleshooterlinks` (`troubleshootercatid`, `parenttroubleshooterid`, `childtroubleshooterid`) VALUES ". implode(",", $linksql).";");
	}
}
?>